<template>
    <view :class="`paimaiitem item state${objInfo.orderstate} ${checkOrderTime(objInfo)} ${objInfo.success_num > 0 ? 'success' : ''}`" v-if="objInfo && objInfo.id">
        <view class="store flexb">
            <view class="store_name flexb">
                <image class="icon" :src="imgpath + 'store_icon.png?v=' + imgVersion" mode="aspectFit" > </image>
                <view class="txt">{{ objInfo.store.company }}</view>
            </view>
            <view class="stateInfo" v-if="objInfo.orderstate > 1 || checkOrderTime(objInfo) == 'withIn'">{{getState(objInfo).text }}</view>
            <view class="stateInfo" v-else>{{djsObj.dd ? djsObj.dd + '天' : ''}}{{ `${djsObj.hh}:${djsObj.mm}:${djsObj.ss}` }}开拍</view>
        </view>
        <view :class="`order-content `">
            <view class="prosbox" @click="linkTo('detail')">
                <view class="pros_detail flexb">
                    <view class="pro-img ">
                        <image class="" :src="objInfo.product.litpic || defaultPro" mode="aspectFill"  @error="objInfo.product.litpic = defaultPro "></image>
                        <view class="pro_mask" v-if="objInfo.orderstate == 1">{{ checkOrderTime(objInfo) == 'withIn' ? '竞拍中' : '待开拍' }}</view>
                    </view>
                    
                    <view class="pro-info ">
                        <view class="pro_detail flexb">
                            <view class="flex_l">
                                <view class="pro-tit">{{ objInfo.product.title }}</view>
                                <view :class="`currMoney `">{{objInfo.orderstate && [3,4,5,6,7].includes(objInfo.orderstate) ? '成交价' : '当前价'}} <text class="symbol">{{ globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥' }}</text><text class="b">{{ objInfo.product.money }}</text></view>
                            </view>
                            <view class="flex_r">
                                <view class="num">x{{ objInfo.procount }}</view>
                            </view>
                        </view>
                    </view>
                </view>
                <view class="order_detail">
                    <view class="bzj">{{ objInfo.product.bao_money > 0 ? `已交保：${globalData.appConfig.currency && globalData.appConfig.currency.symbol || '￥'}${objInfo.product.bao_money}` : '免保证金' }}</view>

                    <view v-if="objInfo.orderstate > 3 && objInfo.orderstate != 5" class="flexb payMoney">
                        {{[3,4,6].includes(objInfo.orderstate) ? '已付' : ''}}尾款 <view class="money ">{{ globalData.appConfig.currency ?  globalData.appConfig.currency.symbol : '￥'}}<view class="b">{{ (objInfo.product.money.toString()).split('.')[0] }}<text v-if="(objInfo.product.money.toString()).includes('.')">.{{ (objInfo.product.money.toString()).split('.')[1] }}</text></view></view>
                    </view>
                </view>
            </view>
            <view class="btnsbox" v-if="objInfo.orderstate != 5 || objInfo.success_num == 0">
                <view class="btn" v-if="objInfo.orderstate == 1" @click="linkTo('predetail')">竞拍详情</view>
                <view class="btn borderbtn" v-else-if="[3,4,6].includes(objInfo.orderstate)" @click="linkTo('detail')">查看物流</view>
                <view class="btn borderbtn" v-else-if="objInfo.orderstate == 5 && objInfo.success_num == 0" @click="linkTo('detail')">查看退款</view>
                <template v-else-if="objInfo.orderstate == 7">
                    <view class="btn" v-if="objInfo.product.jy_type == 0"  @click="linkTo('pay')">立即支付 <text> {{djsObj.dd > 0 ? djsObj.dd + '天' : ''}}{{ `${djsObj.hh}:${djsObj.mm}:${djsObj.ss}` }}</text></view>
                    <view class="btn" v-else  @click="linkTo('contact')">联系商家支付</view>
                </template>
               

            </view>
        </view>
    </view>
</template>

<script>
const app = getApp()
export default {
    props:{
        objInfo:{
            type:Object,
            default:{
                
            }
        },
        ind:{
            type:Number,
            default:0
        }
    },
    computed:{
        checkOrderTime(){
            return function(order){
                const that = this;
                if(!order) return '';
                let nowTime = parseInt(new Date().valueOf() / 1000)
                let clasName = ''
                if(nowTime < order.product.startdate){
                    clasName = 'preStart'
                }else if(nowTime >= order.product.startdate && nowTime <= order.product.enddate){
                    clasName = 'withIn'
                }else if(nowTime > order.product.enddate){
                    clasName = 'afterEnd'
                }
                return clasName
            }
        },
        getState(){
            return function(order){
                const that = this;
                let orderstate = order.orderstate;
                let stateInfo = '',clsName = ''
                switch(orderstate){
                    case 1:
                        stateInfo = '竞拍中'
                        break;
                    case 2:
                        stateInfo = ''
                        break;
                    case 7:
                        stateInfo = '竞拍成功！请及时付款'
                        break;
                    case 5:
                        stateInfo = '竞拍失败'
                        if(order.success_num > 0){
                            stateInfo = '超时未付，已违约';//中拍，未付款
                        }
                        break;
                    case 3:
                        stateInfo = '待发货'
                        break;
                    case 4:
                        stateInfo = '已完成'
                        break;
                    case 6:
                        stateInfo = '配送中'
                        break;
                }

                return {
                    text:stateInfo,
                    cls:clsName
                }
            }
        },
    },
    data(){
        return{
            defaultPro:app.globalData.masterDomain + '/static/images/good_default.png',
            globalData:app.globalData,
            platForm:app.globalData.platForm,
            imgpath:app.globalData.imgPath + '/member/order/',
            masterDomain:app.globalData.masterDomain,
            imgVersion:app.globalData.imgVersion,
            djsObj:{
                dd:'0',
                hh:'00',
                mm:'00',
                ss:'00',
            },
            interval:null
        }
    },
    mounted(){
        const that = this;
        that.getCutDome()
    },
    methods:{
        getCutDome(){
            const that = this;
            if(that.interval){
                clearInterval(that.interval); //清除定时器
            }
            if(that.objInfo.orderstate == 1 && that.objInfo.product.jy_type == 0 ){
                let nowTime = parseInt(new Date().valueOf() / 1000)
                if(nowTime < that.objInfo.product.startdate){
                    
                    that.interval = setInterval(() => {
                        let off = that.objInfo.product.startdate - nowTime;
                        nowTime = parseInt(new Date().valueOf() / 1000)
                        if( off < 0) {
                            clearInterval(that.interval);
                            return false
                        }
                        let dd =  parseInt(off / 60 / 60 / 24);
                        let hh = parseInt(off / 60 / 60  % 24);
                            hh = hh > 9 ? hh : ('0' + hh)
                        let mm = parseInt(off / 60 % 60)
                            mm = mm > 9 ? mm : ('0' + mm)
                        let ss = parseInt(off % 60)
                            ss = ss > 9 ? ss : ('0' + ss)
                        that.djsObj  = {
                            dd:dd,
                            hh:hh,
                            mm:mm,
                            ss:ss
                        }

                    },1000)
                }
            }else if(that.objInfo.orderstate == 7){
                let nowTime = parseInt(new Date().valueOf() / 1000)
                let endtime = that.objInfo.product.enddate + that.objInfo.product.pay_limit * 60 * 60
                if(nowTime < endtime){
                    that.interval = setInterval(() => {
                        let off = endtime - nowTime;
                        nowTime = parseInt(new Date().valueOf() / 1000)
                        if( off < 0) {
                            clearInterval(that.interval);
                            return false
                        }
                        let dd =  parseInt(off / 60 / 60 / 24);
                        let hh = parseInt(off / 60 / 60  % 24);
                            hh = hh > 9 ? hh : ('0' + hh)
                        let mm = parseInt(off / 60 % 60)
                            mm = mm > 9 ? mm : ('0' + mm)
                        let ss = parseInt(off % 60)
                            ss = ss > 9 ? ss : ('0' + ss)
                        that.djsObj  = {
                            dd:dd,
                            hh:hh,
                            mm:mm,
                            ss:ss
                        }

                    },1000)
                }
            }
        },

        // 按钮点击/链接跳转
        linkTo(type){
            const that = this;
            if(type == 'detail'){
                let url =  `${app.globalData.appConfig.cfg_user_index}/orderdetail-paimai-${that.objInfo.id}`
                app.linkTo(url,false,false)
            }else if(type == 'predetail'){
                let url =  `${app.globalData.masterDomain}/paimai/detail-${that.objInfo.product.proid}`
                app.linkTo(url,false,false)
            }else if(type == 'pay'){
                uni.$emit('payorder',{objInfo:this.objInfo,ind:this.ind})
            }else if(type == 'contact'){
                if(app.globalData.appBoolean && app.globalData.platForm == 'web'){
                    window.location.href = 'tel:' + that.objInfo.store.tel
                }else{
                    uni.makePhoneCall({
						phoneNumber: that.objInfo.store.tel //仅为示例
					});
                }
            }
        }
    }

}
</script>
<style scoped>
 @import url(./orderlist_paimai.css);
</style>